@echo off
echo ========================================
echo WeCollege 云函数到服务器API迁移脚本
echo ========================================
echo.

:: 检查是否在正确目录
if not exist "miniprogram" (
    echo 错误: 请在WeCollege项目根目录下运行此脚本
    pause
    exit /b 1
)

echo 开始迁移...
echo.

:: 1. 备份并替换核心文件
echo 1. 替换核心文件...

:: 备份原文件
if exist "miniprogram\app.js" (
    copy "miniprogram\app.js" "miniprogram\app_old.js" >nul
    echo 已备份: app.js
)

if exist "miniprogram\helper\cloud_helper.js" (
    copy "miniprogram\helper\cloud_helper.js" "miniprogram\helper\cloud_helper_old.js" >nul
    echo 已备份: cloud_helper.js
)

if exist "miniprogram\comm\biz\passport_biz.js" (
    copy "miniprogram\comm\biz\passport_biz.js" "miniprogram\comm\biz\passport_biz_old.js" >nul
    echo 已备份: passport_biz.js
)

if exist "miniprogram\helper\content_check_helper.js" (
    copy "miniprogram\helper\content_check_helper.js" "miniprogram\helper\content_check_helper_old.js" >nul
    echo 已备份: content_check_helper.js
)

if exist "miniprogram\comm\biz\admin_biz.js" (
    copy "miniprogram\comm\biz\admin_biz.js" "miniprogram\comm\biz\admin_biz_old.js" >nul
    echo 已备份: admin_biz.js
)

if exist "miniprogram\comm\biz\fav_biz.js" (
    copy "miniprogram\comm\biz\fav_biz.js" "miniprogram\comm\biz\fav_biz_old.js" >nul
    echo 已备份: fav_biz.js
)

:: 替换为新文件
if exist "miniprogram\app_new.js" (
    copy "miniprogram\app_new.js" "miniprogram\app.js" >nul
    echo 已替换: app.js
)

if exist "miniprogram\helper\server_helper.js" (
    copy "miniprogram\helper\server_helper.js" "miniprogram\helper\cloud_helper.js" >nul
    echo 已替换: cloud_helper.js -> server_helper.js
)

if exist "miniprogram\comm\biz\passport_biz_new.js" (
    copy "miniprogram\comm\biz\passport_biz_new.js" "miniprogram\comm\biz\passport_biz.js" >nul
    echo 已替换: passport_biz.js
)

if exist "miniprogram\helper\content_check_helper_new.js" (
    copy "miniprogram\helper\content_check_helper_new.js" "miniprogram\helper\content_check_helper.js" >nul
    echo 已替换: content_check_helper.js
)

if exist "miniprogram\comm\biz\admin_biz_new.js" (
    copy "miniprogram\comm\biz\admin_biz_new.js" "miniprogram\comm\biz\admin_biz.js" >nul
    echo 已替换: admin_biz.js
)

if exist "miniprogram\comm\biz\fav_biz_new.js" (
    copy "miniprogram\comm\biz\fav_biz_new.js" "miniprogram\comm\biz\fav_biz.js" >nul
    echo 已替换: fav_biz.js
)

echo.
echo 2. 替换表白墙页面文件...

:: 替换表白墙页面
if exist "miniprogram\projects\campus\pages\board\add\board_add_new.js" (
    if exist "miniprogram\projects\campus\pages\board\add\board_add.js" (
        copy "miniprogram\projects\campus\pages\board\add\board_add.js" "miniprogram\projects\campus\pages\board\add\board_add_old.js" >nul
    )
    copy "miniprogram\projects\campus\pages\board\add\board_add_new.js" "miniprogram\projects\campus\pages\board\add\board_add.js" >nul
    echo 已替换: board_add.js
)

if exist "miniprogram\projects\campus\pages\board\index\board_index_new.js" (
    if exist "miniprogram\projects\campus\pages\board\index\board_index.js" (
        copy "miniprogram\projects\campus\pages\board\index\board_index.js" "miniprogram\projects\campus\pages\board\index\board_index_old.js" >nul
    )
    copy "miniprogram\projects\campus\pages\board\index\board_index_new.js" "miniprogram\projects\campus\pages\board\index\board_index.js" >nul
    echo 已替换: board_index.js
)

if exist "miniprogram\projects\campus\pages\board\detail\board_detail_new.js" (
    if exist "miniprogram\projects\campus\pages\board\detail\board_detail.js" (
        copy "miniprogram\projects\campus\pages\board\detail\board_detail.js" "miniprogram\projects\campus\pages\board\detail\board_detail_old.js" >nul
    )
    copy "miniprogram\projects\campus\pages\board\detail\board_detail_new.js" "miniprogram\projects\campus\pages\board\detail\board_detail.js" >nul
    echo 已替换: board_detail.js
)

if exist "miniprogram\projects\campus\biz\board_biz_new.js" (
    if exist "miniprogram\projects\campus\biz\board_biz.js" (
        copy "miniprogram\projects\campus\biz\board_biz.js" "miniprogram\projects\campus\biz\board_biz_old.js" >nul
    )
    copy "miniprogram\projects\campus\biz\board_biz_new.js" "miniprogram\projects\campus\biz\board_biz.js" >nul
    echo 已替换: board_biz.js
)

echo.
echo ========================================
echo 迁移完成！
echo ========================================
echo.
echo 重要提醒:
echo 1. 请在 miniprogram\setting\api_config.js 中配置您的服务器地址
echo 2. 原文件已备份为 _old.js 后缀
echo 3. 如有问题可以使用备份文件恢复
echo 4. 请测试所有功能确保正常工作
echo.
echo 下一步操作:
echo 1. 修改 api_config.js 中的服务器地址
echo 2. 在微信开发者工具中预览测试
echo 3. 测试登录、发帖、评论等核心功能
echo.
pause